package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.SlideListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.AddBankCardActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.SelectBankCardActivityAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.SelectBankCardModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/22.
 * 选择银行卡
 */

public class SelectBankCardActivity extends Activity implements View.OnClickListener,Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.bottom_options)//添加银行卡
    LinearLayout bottom_options;


    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;

    Context context;
    @Bind(R.id.listview)
    SlideListView listview;

    private List<SelectBankCardModel> bankCard;
    SelectBankCardActivityAdapter adapter;
    private MyProgressDialog mDialog;
    String Judge_interface="0";//判断哪个接口   0、获取银行卡  1、选择银行卡 2、删除
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_bank_card);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        bottom_options.setOnClickListener(this);
        adapter=new SelectBankCardActivityAdapter(this);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(clickListener);

    }
    AdapterView.OnItemClickListener clickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            processacquiescence(bankCard.get(i).getId());
        }
    };
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
    private  void initData(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
                Judge_interface="0";
                HttpClient.banklist(this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //删除
    public void processdeletingr(String id){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
                Judge_interface="2";
                HttpClient.deleBank(id,this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //默认银行
    public void processacquiescence(String id){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            Judge_interface="1";
            HttpClient.settingbank(id,this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.bottom_options:
                Judge_interface="0";
                startActivity(new Intent(context,AddBankCardActivity.class));
                break;


        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5"+result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                if (Judge_interface.equals("0")) {//显示银行卡
                    String content = getnote.get(0).getContent();
                    bankCard = JsonTools.getBankCard(content);
                    if (bankCard != null || bankCard.size() > 0) {
                        adapter.addItem(bankCard);
                        UserInfoUtils.getInstans().updatebankNumber(context,""+bankCard.size());
                    }
                    available_layout.setVisibility(View.GONE);
                }else if (Judge_interface.equals("1")){

                    ToastUtils.show(context, getMsg);
                    initData();
                }
            }else if (status.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            }else {
                if (Judge_interface.equals("0")) {
                    available_layout.setVisibility(View.VISIBLE);
                }
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
